package com.cyclingAssistant.project;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

public class SavedRoutes extends ListActivity {
	private List<Map<String, Object>> mData;
	private MyAdapter adapter;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mData = getData();
		adapter = new MyAdapter(this);
		setListAdapter(adapter);
	}

	private List<Map<String, Object>> getData() {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("item_name", "2011-11-24, auto saved");
		list.add(map);
		
		map = new HashMap<String, Object>();
		map.put("item_name", "To Work, flat");
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("item_name", "Nice Day");
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("item_name", "Good workout");
		list.add(map);

		return list;
	}

	// ListView 中某项被选中后的逻辑

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
//		Log.i("Addressbook-click", (String)mData.get(position).get("item_name"));
		// Toast.makeText(getApplicationContext(), ((TextView) v).getText(),
		// Toast.LENGTH_SHORT).show();
		showDialog();
	}
	protected void showDialog()
	{
		LayoutInflater inflater = LayoutInflater.from(this.getParent());
		final View textEntry = inflater.inflate(R.layout.route_dialog, null);
		
		final AlertDialog.Builder builder = new Builder(this.getParent());
		builder.setCancelable(false);
		builder.setTitle("Edit route");
		builder.setView(textEntry);
		builder.setPositiveButton("Save", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
			}
		});

		builder.setNegativeButton("cancel", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
			}
		});
		builder.show();
	}
	/**
	 * listview中点击按键弹出对话框
	 * @param position 
	 */
	public void showInfo(final int position) {
		new AlertDialog.Builder(this.getParent()).setTitle("Warning")
				.setMessage("Are you sure to delete?")
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						mData.remove(position);
						adapter.notifyDataSetChanged();
					}
				}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				}).show();

	}

	public final class ViewHolder {
		public TextView routesView;
		public ImageButton deleteButton;
	}

	public class MyAdapter extends BaseAdapter {

		private LayoutInflater mInflater;

		public MyAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mData.size();
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {

			ViewHolder holder = null;
			if (convertView == null) {

				holder = new ViewHolder();

				convertView = mInflater.inflate(R.layout.list_item, null);
				holder.routesView = (TextView) convertView
						.findViewById(R.id.item_name);
				holder.deleteButton = (ImageButton) convertView
						.findViewById(R.id.item_delete_button);
				convertView.setTag(holder);

			} else {

				holder = (ViewHolder) convertView.getTag();
			}

			holder.routesView.setText((String) mData.get(position).get(
					"item_name"));

			holder.deleteButton.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					showInfo(position);
				}
			});

			return convertView;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mData.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

	}
}
